/*
 * @Author: szx
 * @Date: 2021-11-28 13:38:42
 * @LastEditTime: 2021-11-28 13:51:38
 * @Description:
 * @FilePath: \leetcode\1900-1999\1991\1991.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
var pivotIndex = function (nums) {
    let sum = 0;
    for (let i of nums) {
        sum += i;
    }
    let tmp = 0;
    sum -= nums[0];
    if (sum == 0) return 0;
    for (let i = 1; i < nums.length; i++) {
        tmp += nums[i - 1];
        sum -= nums[i];
        if (sum == tmp) return i;
    }
    return -1;
};

console.log(pivotIndex([-1, 1, 2]));
